import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import config from '../global/config'

axios.defaults.baseURL = config.SERVER;



Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        group: JSON.parse(localStorage.getItem('group')) || null,
        token: null,
        user: null,
        post: null,
        error: null,
        successInfo: null,
    },
    getters: {
        isLogin: state => {
            return !!state.token;
        }
    },
    mutations: {
        SET_TOKEN(state, token) {
            state.token = token;
            localStorage.setItem('isLogin', 1);
            localStorage.setItem('token', token);
        },
        SET_USER(state, user) {
            state.user = user;
            localStorage.setItem('user', JSON.stringify(user));
        },
        SET_GROUP(state, group) {
            state.group = group;
            localStorage.setItem('group', JSON.stringify(group));
        }
    },
    actions: {
        login({ commit }, user) {
            return axios.post('user/login', user).then(res => {
                commit('SET_TOKEN', res.data.token)
                commit('SET_USER', res.data.user);
            })
        }

    },
    modules: {}
})